package cn.jbooter.dubbo.autoconfigure.config;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import cn.jbooter.dubbo.autoconfigure.rpc.filter.ConsumerFilter;
import cn.jbooter.dubbo.autoconfigure.rpc.filter.ConsumerFilterBean;
import cn.jbooter.dubbo.autoconfigure.rpc.filter.ProviderFilter;
import cn.jbooter.dubbo.autoconfigure.rpc.filter.ProviderFilterBean;

/**
 * jbooter过滤器组件配置
 * @author hejian
 *
 */
public class JbooterFilterConfigure {
	private static final Logger logger = LoggerFactory.getLogger(JbooterFilterConfigure.class);
	
	@Autowired
	private ApplicationContext applicationContext;
	
	/**
	 * spring容器初始化完成后,注册自定义的dubbo拦截器
	 * @param applicationContext
	 */
	@PostConstruct
	public void registerJbooterFilter() {
		try {
			Map<String, ConsumerFilter> consumerFilters = applicationContext.getBeansOfType(ConsumerFilter.class);
			if(consumerFilters!=null && !consumerFilters.isEmpty()) {
				ConsumerFilterBean.setConsumerFilter(consumerFilters);
			}else {
				logger.debug("没有自定义的ConsumerFilter被找到.");
			}
		} catch (NoSuchBeanDefinitionException e) {
			logger.debug("没有自定义的ConsumerFilter被找到.");
		}
		try {
			Map<String, ProviderFilter> providerFilters = applicationContext.getBeansOfType(ProviderFilter.class);
			if(providerFilters!=null && !providerFilters.isEmpty()) {
				ProviderFilterBean.setProviderFilter(providerFilters);
			}else {
				logger.debug("没有自定义的ProviderFilter被找到.");
			}
			
		} catch (NoSuchBeanDefinitionException e) {
			logger.debug("没有自定义的ProviderFilter被找到.");
		}
	}
	
}
